import util from "../../util";
import parent from '../parent/parent';

export default class bi extends parent{
    constructor(path) {
        super(bi, util.api.Bi);
    }
    static create(record, path){
        let object=new bi(path);
        if(!!record){
            object.setRecord(record);
        };
        return object;
    }
    /***
     * 新增
     * @param record
     * @returns {Promise<boolean|*>}
     */
    async add(record=this.record){
        console.log("file: bi.js:21  bi  add  record:", record)
        if(!record||!record.type||!record.name||record.type==''||record.name==''){
            return false;
        }
        let newRecord=Object.assign(this.clz.tpl(),record);
        let result = await this.api.add(newRecord).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    async update(record=this.record){
        let result = await this.api.update(record).then(data=>{
            if(!!data){
                this.setRecord(data);
                return true;
            }else{
                return false;
            }
        }).catch(err=>{
            return false;
        });
        return result;
    }
    /* 新增模板 */
    static tpl(){
        return {
            id: null,
            type: "",
            name: "",
            web: {},
            version: "1.0",
            published: true,
            comments: "",
        }
    }
}
